<?php
/**
 * @Author: liuhongbin
 * @Email: 514660094@qq.com
 * @Date: 2019-02-12 16:15:10
 * @Description:
 * @LastEditors: liuhongbin
 * @LastEditTime: xxx
 */

namespace addons\gs_developer\controller;

use addons\gs_developer\model\EventModel;
use addons\gs_xsy\util\FormBuilder\AddonFormBuilder as Form;
use app\common\controller\AddonsController;
use app\common\services\facade\Addons;
use think\App;

class Event extends AddonsController
{
    protected $model;

    public function __construct(App $app)
    {
        parent::__construct($app);
        $this->model = new EventModel();
    }

    public function index()
    {
        // 获取列表数据
        $res = $this->model->getPageList();
        return $this->success($res['count'], $res['data']);
    }

    public function getEventSettingForm()
    {
        //创建表单
        $rule = array();
        $gid = input('gid', 0);
        $row = $this->model->findOrEmpty($gid);//创建表单
        $form = Form::createForm("/gs_developer/admin.group/save?gid=" . $gid)->setMethod("POST");
        $rule[] = Form::input('title', '项目组名称', $row['title'] ?? '')->required();
        $title = !$gid ? "新增" : "编辑";
        $form->setTitle($title)->setRule($rule);
        return $this->success("", Form::getFormData($form), $row);
    }

    // 事件复现
    public function eventReplay($id)
    {
        $this->request->eventReplay = true;
        $this->request->eventReplayId = $id;
        $event = $this->model->findOrEmpty($id);
        if ($event->isEmpty()) {
            return $this->error("事件不存在");
        }
        $res = event($event['name'], $event['value']);
        return $this->success('复现成功', $res);
    }

    // 删除复现事件
    public function deleteReplay()
    {
        $this->model->where('replay', '>',0)->delete();
        return $this->success('删除成功');
    }

    public function clear()
    {
        $sql = "TRUNCATE  table __PREFIX__gs_developer_event;";
        Addons::batchSql($sql);
        return $this->success('清空成功');
    }

    public function delete($id)
    {
        $this->model->where('id', $id)->delete();
        return $this->success('删除成功');
    }
}
